package com.ctms.system.controller;

import com.ctms.common.utils.PageUtils;
import com.ctms.common.utils.Query;
import com.ctms.common.utils.ShiroUtils;
import com.ctms.system.domain.*;
import com.ctms.system.service.CourseScoreService;
import com.ctms.system.service.CourseService;
import com.ctms.system.service.CourseStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@RequestMapping("/courseScore")
@Controller
public class CourseScoreController {

    @Autowired
    CourseScoreService courseScoreService;
    @Autowired
    CourseService courseService;
    @Autowired
    CourseStudentService courseStudentService;

    @GetMapping("")
    public String courseScore(){
        return "sys/courseScore/courseScore";
    }

    @GetMapping("/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String,Object> params){
        UserDO user= ShiroUtils.getUser();
        Integer loginId = Integer.parseInt(Long.toString(user.getUserId()));
        params.put("loginId", loginId);
        Query query = new Query(params);
        List<CourseScoreDO> courseScoreDOS = courseScoreService.selectAllCourseScore(params);
        for (int i=0;i<courseScoreDOS.size();i++){
            //作业总成绩sum
            int sum=0;
            String name = courseScoreDOS.get(i).getCourseName();
            //1、通过课程名去任务表查出这门课的所有任务tid
            List<TaskReleaseDO> taskReleaseDOS = courseScoreService.selectAllTaskRelease(name);
            //2、通过tid和登录人的userId去成绩表查询这门课目前的所有得分
            for (int j=0;j<taskReleaseDOS.size();j++){
                int tid = taskReleaseDOS.get(j).getTid();
                CorrectsTaskDO correctsTaskDO = courseScoreService.selectScoreByTidUserId(tid,loginId);
                if (correctsTaskDO!=null){
                    //每次作业的成绩
                    if (correctsTaskDO.getScore() != null ){
                        int score = correctsTaskDO.getScore();
                        sum = sum + score;
                    }
                }
            }
            //作业成绩
            courseScoreDOS.get(i).setTaskScore(sum);
            //打卡成绩
            int cardScore = 0;
            CourseDO courseDO = courseService.selectCourseByCid(name);
            int cid = courseDO.getCid();
            CourseStudentDO courseStudentDO = courseStudentService.selectCourseStuScore(cid,loginId);
            //打卡天数
            int card_score = courseStudentDO.getCardNum();
            courseScoreDOS.get(i).setCardScore(cardScore+card_score);
            int card = courseScoreDOS.get(i).getCardScore();
            //总成绩
            courseScoreDOS.get(i).setScore(card+sum);
            //更新course_score
            courseScoreService.updateCourseScore(courseScoreDOS.get(i));
        }
        int total = courseScoreService.count(query);
        PageUtils pageUtils = new PageUtils(courseScoreDOS,total);
        return pageUtils;
    }


}
